using MicroMsg.Common.Utils;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
namespace MicroMsg.Manager
{
	public class SnsRecycleMgr
	{
		private enum RecycleOperat
		{
			none,
			snsInfo,
			snsPage,
			snsNotify,
			snsFile
		}
		private const string TAG = "SnsRecycleMgr";
		private const int SNS_OBJECT_RESERVE_NUM = 80;
		private const int SNS_OBJECT_MAX_NUM = 2000;
		private const int LARGE_MEDIA_FILE = 20480;
		private const int MINUTES_PER_DAY = 1440;
		private const int MAX_FIlE_SIZE = 104857600;
		private const int RESERVE_FIlE_SIZE = 10485760;
		private const int MAX_FIlE_NUM = 3000;
		private const int RESERVE_FILE_NUM = 10;
		private const int RESERVE_UPLOAD_FILE_NUM = 50;
		private const int MAX_CHECK_TIME_DAYS = 5;
		private const int MAX_RECYCLE_TIME = 180;
		private const bool bTestRecycleFlag = false;
		private const string lastRecycleDayKey = "lastRecycleDayKey";
		private const int db_item_del_number = 400;
		public static int checkCount = 0;
		private static SnsRecycleMgr.RecycleOperat curOper;
		public static List<string> dirsList;
		private static Action completeCB;
		private static long recycleStartTime = 0L;
		private static int filesLeft = 0;
		private static bool needClearFile()
		{
			SnsMediaInfo mediaInfo = SnsMediaStorage.getMediaInfo();
			return mediaInfo.fileNumber >= 3000;
		}
		private static bool needClearDB()
		{
			return StorageMgr.snsInfo.count() >= 2000;
		}
		public static bool needDoRecycle()
		{
			if (SnsRecycleMgr.checkCount > 0)
			{
				return false;
			}
			SnsRecycleMgr.checkCount++;
			int @int = ConfigMgr.getInt("lastRecycleDayKey", 0);
			int num = Math.Abs(Util.getNowDays() - @int);
			return num >= 5 && (SnsRecycleMgr.needClearFile() || SnsRecycleMgr.needClearDB());
		}
		private static void fileRecycle()
		{
		}
		public static bool isRecycling()
		{
			return SnsRecycleMgr.curOper != SnsRecycleMgr.RecycleOperat.none;
		}
		public static int doRecycleStart(Action onComplete)
		{
			if (SnsRecycleMgr.curOper != SnsRecycleMgr.RecycleOperat.none)
			{
				return 0;
			}
			Log.smethod_5("SnsRecycleMgr", "doRecycleStart");
			ConfigMgr.setInt("lastRecycleDayKey", Util.getNowDays());
			SnsRecycleMgr.completeCB = onComplete;
			SnsRecycleMgr.recycleStartTime = DateTime.get_Now().get_Ticks();
			int num = StorageMgr.snsInfo.count();
			if (num >= 2000)
			{
				SnsRecycleMgr.curOper = SnsRecycleMgr.RecycleOperat.snsInfo;
				return SnsRecycleMgr.doRecycleStep();
			}
			SnsMediaInfo mediaInfo = SnsMediaStorage.getMediaInfo();
			if (mediaInfo.fileNumber >= 3000)
			{
				SnsRecycleMgr.curOper = SnsRecycleMgr.RecycleOperat.snsFile;
				return SnsRecycleMgr.doRecycleStep();
			}
			return SnsRecycleMgr.onRecycLeComplete();
		}
		private static int doRecycleStep()
		{
			Log.smethod_5("SnsRecycleMgr", "RecycleOperat =" + SnsRecycleMgr.curOper);
			if (SnsRecycleMgr.curOper == SnsRecycleMgr.RecycleOperat.none)
			{
				SnsRecycleMgr.onRecycLeComplete();
				return 0;
			}
			long num = (long)TimeSpan.FromTicks(DateTime.get_Now().get_Ticks() - SnsRecycleMgr.recycleStartTime).get_TotalSeconds();
			if (num >= 180L)
			{
				SnsRecycleMgr.onRecycLeComplete();
				return 0;
			}
			switch (SnsRecycleMgr.curOper)
			{
			case SnsRecycleMgr.RecycleOperat.snsInfo:
			{
				int num2 = StorageMgr.snsInfo.count();
				if (num2 <= 80)
				{
					SnsRecycleMgr.curOper++;
				}
				else
				{
					int num3 = num2 - 80;
					if (num3 > 400)
					{
						num3 = 400;
					}
					List<SnsInfo> list = StorageMgr.snsInfo.getList(80, num3);
					if (list.get_Count() > 0)
					{
						StorageMgr.snsInfo.delList(list);
					}
				}
				break;
			}
			case SnsRecycleMgr.RecycleOperat.snsPage:
			{
				List<SnsPage> list2 = StorageMgr.snsPage.getList();
				if (list2 != null && list2.get_Count() > 2)
				{
					SnsPage snsPage = Enumerable.FirstOrDefault<SnsPage>(Enumerable.Where<SnsPage>(list2, (SnsPage snsPage_0) => snsPage_0.strUserName == SnsPageMgr.TimeLineUserName));
					SnsPage snsPage2 = Enumerable.FirstOrDefault<SnsPage>(Enumerable.Where<SnsPage>(list2, (SnsPage snsPage_0) => snsPage_0.strUserName == AccountMgr.strUsrName));
					if (snsPage != null)
					{
						list2.Remove(snsPage);
					}
					if (snsPage2 != null)
					{
						list2.Remove(snsPage2);
					}
					if (list2.get_Count() > 400)
					{
						list2 = Enumerable.ToList<SnsPage>(Enumerable.Take<SnsPage>(list2, 400));
					}
					StorageMgr.snsPage.delList(list2);
				}
				else
				{
					SnsRecycleMgr.curOper++;
				}
				break;
			}
			case SnsRecycleMgr.RecycleOperat.snsNotify:
			{
				SnsPageMgr.notifySnsRecycled();
				SnsMediaInfo mediaInfo = SnsMediaStorage.getMediaInfo();
				if (mediaInfo.fileNumber < 3000)
				{
					return SnsRecycleMgr.onRecycLeComplete();
				}
				SnsRecycleMgr.curOper++;
				break;
			}
			case SnsRecycleMgr.RecycleOperat.snsFile:
			{
				if (SnsRecycleMgr.dirsList == null)
				{
					SnsRecycleMgr.dirsList = StorageIO.dirsDetail(StorageIO.getSnsPath());
					SnsRecycleMgr.filesLeft = 0;
				}
				if (SnsRecycleMgr.dirsList == null || SnsRecycleMgr.dirsList.get_Count() <= 0)
				{
					return SnsRecycleMgr.onRecycLeComplete();
				}
				long ticks = DateTime.get_Now().get_Ticks();
				while (TimeSpan.FromTicks(DateTime.get_Now().get_Ticks() - ticks).get_TotalMilliseconds() < 2000.0)
				{
					if (SnsRecycleMgr.dirsList.get_Count() <= 0)
					{
						return SnsRecycleMgr.onRecycLeComplete();
					}
					string text = SnsRecycleMgr.dirsList.get_Item(0);
					SnsRecycleMgr.dirsList.RemoveAt(0);
					List<FileDetail> list3 = StorageIO.filesDetail(text);
					if (list3 != null && list3.get_Count() > 0)
					{
						if (SnsMediaStorage.isUploadPath(text))
						{
							if (list3.get_Count() <= 50)
							{
								continue;
							}
							list3 = Enumerable.ToList<FileDetail>(Enumerable.Take<FileDetail>(Enumerable.OrderByDescending<FileDetail, long>(list3, (FileDetail fileDetail_0) => fileDetail_0.idleMinutes), list3.get_Count() - 50));
						}
						else
						{
							if (list3.get_Count() <= 10)
							{
								SnsRecycleMgr.filesLeft += list3.get_Count();
								continue;
							}
							SnsRecycleMgr.filesLeft += 10;
							list3 = Enumerable.ToList<FileDetail>(Enumerable.Take<FileDetail>(Enumerable.OrderByDescending<FileDetail, long>(list3, (FileDetail fileDetail_0) => fileDetail_0.idleMinutes), list3.get_Count() - 10));
						}
						using (List<FileDetail>.Enumerator enumerator = list3.GetEnumerator())
						{
							while (enumerator.MoveNext())
							{
								FileDetail current = enumerator.get_Current();
								StorageIO.deleteFile(current.strFileName);
							}
						}
						SnsMediaStorage.setMediaInfo(0L, SnsMediaStorage.getMediaInfo().fileNumber - list3.get_Count());
					}
				}
				if (SnsRecycleMgr.dirsList.get_Count() <= 0)
				{
					SnsMediaStorage.verifyUrlMap();
					SnsMediaStorage.setMediaInfo(0L, SnsRecycleMgr.filesLeft);
					return SnsRecycleMgr.onRecycLeComplete();
				}
				break;
			}
			default:
				return SnsRecycleMgr.onRecycLeComplete();
			}
			Deployment.get_Current().get_Dispatcher().BeginInvoke(delegate
			{
				SnsRecycleMgr.doRecycleStep();
			});
			return 1;
		}
		private static int onRecycLeComplete()
		{
			Log.smethod_5("SnsRecycleMgr", "doRecycle Complete");
			SnsRecycleMgr.curOper = SnsRecycleMgr.RecycleOperat.none;
			if (SnsRecycleMgr.completeCB != null)
			{
				SnsRecycleMgr.completeCB.Invoke();
				SnsRecycleMgr.completeCB = null;
			}
			return 1;
		}
		public static void doRecycLeEnd()
		{
			Log.smethod_5("SnsRecycleMgr", "doRecycle abort");
			SnsRecycleMgr.curOper = SnsRecycleMgr.RecycleOperat.none;
		}
		public static void ClearAll()
		{
			SnsInfoMgr.ClearAll();
			SnsPageMgr.ClearAll();
			SnsMsgMgr.ClearAll();
			SnsMediaStorage.ClearAll();
			StorageMgr.snsTrans.clear(false);
			SnsAsyncMgr.ClearAll();
		}
	}
}
